package com.df.bpm.utils.businessViewerV2.entityType;

import com.df.bpm.utils.businessViewerV2.Main;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Db_entityType {
    Map<Integer,M_entityType> typeMap=new HashMap<>();

    public M_entityType get(Integer typeID) {
        M_entityType result=typeMap.get(typeID);
        if(result==null){//如果本地没存就去接口获取
            RestTemplate restTemplate = new RestTemplate();
            String url= Main.server+"entityType/id?entityTypeIndex="+typeID;
            ResponseEntity<M_entityType> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    new ParameterizedTypeReference<M_entityType>() {}
            );
            result=response.getBody();
            typeMap.put(typeID,result);
        }
        return result;
    }

    public List<M_entityType> getList4Like(M_entityType temp) {
        RestTemplate restTemplate = new RestTemplate();
        String tempString=Main.printNonNullFields(temp);
        String url=Main.server+"entityType/entityType4Like?"+tempString;
        ResponseEntity<List<M_entityType>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_entityType>>() {}
        );
        return response.getBody();
    }

    public Integer update(M_entityType temp) {
        if(temp.getName()!=null
                && !temp.getName().isEmpty()
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"entityType/";
            HttpEntity<M_entityType> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            typeMap.put(temp.getEntityTypeIndex(),temp);
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer delete(Integer entityTypeIndex) {
        if(entityTypeIndex!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"entityType?entityTypeIndex="+entityTypeIndex;
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.DELETE,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            typeMap.remove(entityTypeIndex);
            return (Integer) response.getBody();
        }
        return null;
    }
}
